// Copyright © 2023 ycq_work@163.com. All rights reserved.

/**
 * @Author: ycq_work@163.com
 * @DATE: 2023/10/8
 * @Description: 
*/
#include "MusicPlaybackStatusBar.h"

MusicPlaybackStatusBar::MusicPlaybackStatusBar(QWidget *parent) :
        QWidget(parent)
{
    init();
}
// 生成界面
void MusicPlaybackStatusBar::init()
{
    this->setMinimumHeight(120);
    this->setMaximumHeight(120);
    this->setMinimumWidth(800);
    this->setAttribute(Qt::WA_StyledBackground, true);  // 启用样式表对窗口背景的控制

    QString styleSheet = "background-color: #A0A083;"
                         "border-radius: 7px;";
    this->setStyleSheet(styleSheet);

    // 界面主布局
    auto *layoutMain = new QVBoxLayout;
    // 设置progressBar
    this->progressBar->setRange(0, 100);
    setProgressQss(progressBar, 10, 5, 9, "#AAA0A3", "#EAE0CC");
    this->progressBar->setValue(this->_progressBarInt); // 测试
    this->progressBar->setTextVisible(false);

    auto *layout = new QHBoxLayout;

    // 添加控件
    // 开头结尾的固定长度约束的弹簧
    auto *spacer2 = new QSpacerItem(20, 20, QSizePolicy::Fixed, QSizePolicy::Fixed);
    // 水平弹簧
    auto *spacer = new QSpacerItem(0, 0, QSizePolicy::Expanding, QSizePolicy::Fixed);
    // 竖直弹簧
    auto *spacer1 = new QSpacerItem(0, 0, QSizePolicy::Fixed, QSizePolicy::Expanding);

    // 左边添加一个的弹簧
    layout->addItem(spacer2);

    // 封面
    auto* coverLabel = new QLabel;
    coverLabel->setFixedSize(QSize(75, 75));          // 设置封面标签的固定大小
    coverLabel->setScaledContents(true);                    // 自动缩放图像以适应标签大小
    coverLabel->setPixmap(getPixmapFromImageFile(QString::fromUtf8(":/icons/logo.jpg")));
    layout->addWidget(coverLabel);

    // 编辑歌名队列
    auto *layoutSong = new QVBoxLayout;
    // 歌名队列上面添加一个弹簧
    layoutSong->addItem(spacer1);

    this->songName->setFixedWidth(150);                        // 设置便签长度
    this->songName->setText(this->_songNameText);                 // 设置默认显示名称
    // 字体设置
    QFont font("Microsoft YaHei", 10);              // 创建字体，指定字体名称和字体大小
    songName->setFont(font);
    layoutSong->addWidget(songName);

    // 在中间添加一个竖直方向的弹簧
    layoutSong->addItem(spacer1);

    // 编辑歌名下的按钮控件
    auto *layoutMusicButton = new QHBoxLayout;          // 对歌曲操作按钮编辑
    auto *musicPreferencesBtn = new QPushButton;        // 喜欢按钮
    setPushButtonIcon(musicPreferencesBtn,QString::fromUtf8(":/icons/musicPreferences.svg"));

    auto *downloadBtn = new QPushButton;                 // 下载按钮
    setPushButtonIcon(downloadBtn, QString::fromUtf8(":/icons/download.svg"));

    auto *moreBtn = new QPushButton;                    // 更多按钮
    setPushButtonIcon(moreBtn,QString::fromUtf8(":/icons/more.svg"));

    layoutMusicButton->addWidget(musicPreferencesBtn);
    layoutMusicButton->addWidget(downloadBtn);
    layoutMusicButton->addWidget(moreBtn);

    // 在按钮右边添加一个弹簧
    layoutMusicButton->addItem(spacer);

    layoutSong->addLayout(layoutMusicButton);
    // 在底部添加一个竖直方向的弹簧
    layoutSong->addItem(spacer1);

    layout->addLayout(layoutSong);


    // 添加播放按钮组
    // 添加按钮
    auto* prevTrackButtonBtn = new QPushButton;
    setPushButtonIcon(prevTrackButtonBtn,QSize(35, 35), QSize(35, 35), QString::fromUtf8(":/icons/prevTrackButton.svg"));

    auto* playerBtn = new QPushButton;
    setPushButtonIcon(playerBtn,QSize(50, 50), QSize(50, 50), QString::fromUtf8(":/icons/player.svg"));
    // 在构造函数或初始化函数中连接信号和槽
    connect(playerBtn, &QPushButton::clicked, this, [this, playerBtn]() {
        onPlayerBtnClicked(playerBtn);
    });

    auto* nextTrackButtonBtn = new QPushButton;
    setPushButtonIcon(nextTrackButtonBtn, QSize(35, 35), QSize(35, 35), QString::fromUtf8(":/icons/nextTrackButton.svg"));

    auto *layoutPlayerButton = new QHBoxLayout;          // 对歌曲操作按钮编辑
    layoutPlayerButton->addItem(spacer);
    layoutPlayerButton->addWidget(prevTrackButtonBtn);
    layoutPlayerButton->addItem(spacer);
    layoutPlayerButton->addWidget(playerBtn);
    layoutPlayerButton->addItem(spacer);
    layoutPlayerButton->addWidget(nextTrackButtonBtn);
    layoutPlayerButton->addItem(spacer);

    layout->addItem(spacer);
    layout->addLayout(layoutPlayerButton);


    // 添加右边歌曲列表按钮组
    this->progressBarTime->setText(tr("%1 / %2").arg(this->_nowTime, this->_allTime));

    auto* lyricsTextBtn = new QPushButton;
    setPushButtonIcon(lyricsTextBtn,QString::fromUtf8(":/icons/lyricsText.svg"));

    auto* playlistMusicBtn = new QPushButton;
    setPushButtonIcon(playlistMusicBtn,QString::fromUtf8(":/icons/playlistMusic.svg"));

    auto *layoutPlayerListButton = new QHBoxLayout;          // 对歌曲操作按钮编辑
    layoutPlayerButton->addItem(spacer);
    layoutPlayerListButton->addWidget(progressBarTime);
    layoutPlayerButton->addItem(spacer);
    layoutPlayerListButton->addWidget(lyricsTextBtn);
    layoutPlayerButton->addItem(spacer);
    layoutPlayerListButton->addWidget(playlistMusicBtn);
    layoutPlayerButton->addItem(spacer);

    layout->addItem(spacer);
    layout->addLayout(layoutPlayerListButton);


    // 右边边添加一个的弹簧
    layout->addItem(spacer2);

    layoutMain->addWidget(progressBar);
    layoutMain->addItem(spacer1);
    layoutMain->addLayout(layout);
    layoutMain->addItem(spacer1);

    this->setLayout(layoutMain);
}
// 设置类中数据的值
void MusicPlaybackStatusBar::set_isPlaying(bool isPlaying)
{
    _isPlaying = isPlaying;
}
void MusicPlaybackStatusBar::set_allTime(const QString &allTime)
{
    _allTime = allTime;
}
void MusicPlaybackStatusBar::set_nowTime(const QString &nowTime)
{
    _nowTime = nowTime;
}
void MusicPlaybackStatusBar::set_songNameText(const QString &songNameText)
{
    _songNameText = songNameText;
}
void MusicPlaybackStatusBar::set_progressBarInt()
{
//    this->progressBar = static_cast<int>();
}

QPixmap MusicPlaybackStatusBar::getPixmapFromImageFile(const QString &filePath) {
    QPixmap pixmap;
    if (pixmap.load(filePath)) {
        return pixmap;
    }
    // 加载失败时返回一个空的 QPixmap 对象
    return {};
}

QString MusicPlaybackStatusBar::setProgressQss(QProgressBar *bar, int barHeight, int barRadius, int fontSize,
                                               const QString &normalColor, const QString &chunkColor) {
        QStringList list;
        list.append(QString("QProgressBar{font:%1px;background:%2;max-height:%3px;border-radius:%4px;solid %2;}")
                            .arg(fontSize).arg(normalColor).arg(barHeight).arg(barRadius));
        list.append(QString("QProgressBar:chunk{border-radius:%2px;background-color:%1;}")
                            .arg(chunkColor).arg(barRadius));

        QString qss = list.join("");
        bar->setStyleSheet(qss);
        return qss;
}

void MusicPlaybackStatusBar::onPlayerBtnClicked(QPushButton* playerBtn)
{
    // 切换播放状态和图标
    this->_isPlaying = !_isPlaying;
    if (_isPlaying) {
        QIcon stopIcon;
        stopIcon.addFile(QString::fromUtf8(":/icons/stop.svg"), QSize(), QIcon::Normal, QIcon::Off);
        playerBtn->setIcon(stopIcon);
    }
    else {
        QIcon playIcon;
        playIcon.addFile(QString::fromUtf8(":/icons/player.svg"), QSize(), QIcon::Normal, QIcon::Off);
        playerBtn->setIcon(playIcon);
    }

    // 发送布尔值给Player类
    emit sendIsPlaying(_isPlaying);
}

void MusicPlaybackStatusBar::setPushButtonIcon(QPushButton *button, QSize buttonSize, QSize iconSize, const QString& url)
{
    button->setFixedSize(buttonSize);
    button->setIconSize(iconSize);
    QIcon icon;
    icon.addFile(url, QSize(), QIcon::Normal, QIcon::Off);
    button->setIcon(icon);
}
void MusicPlaybackStatusBar::setPushButtonIcon(QPushButton *button, const QString& url)
{
    QIcon icon;
    icon.addFile(url, QSize(), QIcon::Normal, QIcon::Off);
    button->setIcon(icon);
}

void MusicPlaybackStatusBar::setProgressBarInt(int progressBarInt)
{

}


